﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Quick edge detection filter,
    /// this uses the convolution 3x3 filter to 
    /// create a convoluted pic
    /// </summary>
    public class EdgeDetectQuick : BaseImageFilter
    {
        protected byte m_nOffset = 127;
        
        /// <summary>
        /// default constructor
        /// </summary>
        public EdgeDetectQuick()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EdgeDetectQuick"/> class.
        /// </summary>
        /// <param name="_Offset">The _ offset.</param>
        public EdgeDetectQuick(byte _Offset)
        {
            m_nOffset = _Offset;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_edgeDetectQuick"></param>
        internal EdgeDetectQuick(EdgeDetectQuick _edgeDetectQuick)
        {
        }

        /// <summary>
        /// Gets the offset of this filter or sets it
        /// </summary>
        /// <value></value>
        public new byte Offset
        {
            get { return m_nOffset; }
            set { m_nOffset = value; }
        }

        /// <summary>
        /// edge detection filter for bitmap
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            CConvMatrix matrix = new CConvMatrix();
            matrix.TopLeft = matrix.TopMid = matrix.TopRight = -1;
            matrix.MidLeft = matrix.Pixel = matrix.MidRight = 0;
            matrix.BottomLeft = matrix.BottomMid = matrix.BottomRight = 1;

            matrix.Offset = m_nOffset;

            new Convolution3x3(matrix).Execute(_bitmap);
            return _bitmap;
        }

        /// <summary>
        /// create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new EdgeDetectQuick(this);
        }
    }
}
